home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINSOUND / RANDYSND.ZIP / RANDYSND.CPP < prev    next >
C/C++ Source or Header  |  1992-06-12  |  16KB  |  631 lines

  1. /* randysnd.cpp */
  2.  
  3. // RandySnd is (C) 1992 Anthony McCarthy
  4. //
  5. // Feel free to use and abuse this program to your own ends.
  6. // However, it is provided FREE, AS-IS and with absolutely NO
  7. // WARRANTY whatsoever. Use it at your own risk.
  8. //
  9. // The above statement is especially appropriate as this is
  10. // my very first program built using MSC++ 7 and Microsoft's
  11. // Foundation Classes. As yet I haven't really got my head
  12. // around how to play with precompiled headers. Also, I ain't
  13. // yet played with the diagnostic services provided by MFC
  14. // and so there may well be memory leaks that I haven't seen
  15. // yet.
  16. //
  17. // Please forgive the proliferation of literal integers and
  18. // strings in the code - this code was a quick hack - they
  19. // should all really be symbolic at least.  Also, I haven't yet
  20. // developed/decided on a personal coding style for C++ so 
  21. // things might feel inconsistent.
  22. //
  23. // If you really like this program, a donation of a few pounds,
  24. // dollars, roubles, francs or whatever would be appreciated, to:
  25. // Anthony McCarthy, 14 Beryl Road, Bedminster, Bristol, BS3 3DH, UK
  26. //
  27. // Having said there's no warranty, please report any bugs, enhancement
  28. // requests or source code criticisms to:
  29. //   100012.3712@compuserve.com   
  30. //   amccarthy@cix.compulink.co.uk
  31. //   amc@beryl.demon.co.uk
  32.  
  33.  
  34. // CAVEAT: if you're trying to recompile this source with the same 
  35. // release of MSC 7.0 as I'm using, the compile will fail. Microsoft 
  36. // failed to include the AfxSig_vh enum in afxmsg.h although it is 
  37. // referenced in the ON_WM_DROPFILES() macro in the same file!  To work 
  38. // around this, I simply changed 'AfxSig_vh' to 'AfxSig_vw' in a copy 
  39. // of afxmsg.h.
  40.  
  41.  
  42. // Change History
  43. // ~~~~~~~~~~~~~~
  44. //
  45. // v1.00 : AMcC : 10June92 : initial creation
  46.  
  47.  
  48.  
  49. # include <afxwin.h>
  50. # include <afxcoll.h>
  51. # include <mmsystem.h>
  52. # include <commdlg.h>
  53. # include <shellapi.h>
  54. # include <stdlib.h>
  55. # include "randysnd.h"
  56.  
  57.  
  58.  
  59. static char scratchBuffer[512];
  60.  
  61.  
  62.  
  63. /*--------------------------------------------------------------------------*/
  64. /*                                                                          */
  65. /*                          C M a i n W i n d o w                           */
  66. /*                                                                          */
  67. /*--------------------------------------------------------------------------*/
  68.  
  69. class CMainDialog : public CModalDialog
  70. {
  71.  private:
  72.  
  73.  
  74.    CPtrList soundsList;
  75.  
  76.    BOOL bFilesChanged,
  77.         bAutoRandomiseChanged;
  78.  
  79.    CString curSysSound;
  80.  
  81.    WORD templateId;
  82.  
  83.  public:
  84.    static const char * iniFileName;
  85.  
  86.    CMainDialog(WORD dlgId) : CModalDialog(dlgId) { bFilesChanged = FALSE; 
  87.                                                    bAutoRandomiseChanged = FALSE;
  88.                                                    templateId = dlgId;
  89.                                                  }
  90.    ~CMainDialog();
  91.  
  92.    BOOL OnInitDialog();
  93.  
  94.    BOOL GetIniString(CString section, CString key, CString& value);
  95.    BOOL LoadWavFilesList(const CString & sysSound);
  96.    BOOL SaveWavFilesList(const CString & sysSound);
  97.    BOOL FlushWavFilesList();
  98.    BOOL GetSysSound(CString& sysSound);
  99.    BOOL TryToRandomizeSysSound(CString sysSound);
  100.  
  101.    afx_msg void OnAbout();
  102.    afx_msg void OnDestroy();
  103.    afx_msg void OnSysSndSelChange();
  104.  
  105.    afx_msg void OnFileSelChange();
  106.    afx_msg void OnPlaySound();
  107.    afx_msg void OnAddSoundFile();
  108.    afx_msg void OnRemoveSoundFile();
  109.    afx_msg void OnAutoRandomise();
  110.    afx_msg void OnRandomiseNow();
  111.    afx_msg void OnDropFiles(HDROP hDrop);
  112.  
  113.    DECLARE_MESSAGE_MAP()
  114.  
  115. };
  116.  
  117.  
  118. /*--------------------------------------------------------------------------*/
  119. /*                                                                          */
  120. /*                               C M y A p p                                */
  121. /*                                                                          */
  122. /*--------------------------------------------------------------------------*/
  123.  
  124. class CMyApp : public CWinApp
  125. {
  126.  public:
  127.     int  Run();
  128.     BOOL WantedMinimize();
  129. };
  130.  
  131.  
  132. BOOL CMyApp::WantedMinimize()
  133. {
  134.  return (    m_nCmdShow == SW_SHOWMINIMIZED
  135.           || m_nCmdShow == SW_MINIMIZE
  136.           || m_nCmdShow == SW_SHOWMINNOACTIVE );
  137. }
  138.  
  139.  
  140. int CMyApp::Run()
  141. {
  142.  WORD dlgId;
  143.  
  144.  if (WantedMinimize())
  145.     dlgId = IDD_WORKINPROGRESS;
  146.  else
  147.     dlgId = IDD_MAIN;
  148.  
  149.  CMainDialog mainDialog(dlgId);
  150.  
  151.  mainDialog.DoModal();
  152.  
  153.  return 0;
  154. }
  155.  
  156.  
  157.  
  158. // Static application object... the constructor for
  159. // this kicks everything off. 
  160. //
  161. // This comment is here because even though I find the 
  162. // concept of static intializers simple enough to comprehend,
  163. // its still damned difficult to see where the app 'gets going'.
  164.  
  165. CMyApp myApp;
  166.  
  167.  
  168.  
  169.  
  170. const char * CMainDialog::iniFileName = "randysnd.ini";
  171.  
  172. BEGIN_MESSAGE_MAP( CMainDialog, CModalDialog )
  173.     ON_COMMAND( ID_ABOUT          , OnAbout           )
  174.     ON_COMMAND( ID_PLAYSOUNDFILE  , OnPlaySound       )
  175.     ON_COMMAND( ID_ADDSOUNDFILE   , OnAddSoundFile    )
  176.     ON_COMMAND( ID_REMOVESOUNDFILE, OnRemoveSoundFile )
  177.     ON_COMMAND( ID_AUTORANDOMISE  , OnAutoRandomise   )
  178.     ON_COMMAND( ID_RANDOMIZENOW   , OnRandomiseNow    )
  179.  
  180.     ON_WM_DESTROY( )
  181.     ON_WM_DROPFILES( ) // *SIGH* MS omitted the AfxSig_vh enum from afxmsg.h!
  182.                        // To work round this, I modified the ON_WM_DROPFILES()
  183.                        // macro (in afxmsg.h) to use the AfxSig_vw signature.
  184.  
  185.     ON_CBN_SELCHANGE( ID_SYSTEMSOUNDS, OnSysSndSelChange )
  186.     ON_LBN_SELCHANGE( ID_FILESLIST   , OnFileSelChange )
  187. END_MESSAGE_MAP()
  188.  
  189.  
  190. CMainDialog::~CMainDialog()
  191. {
  192.  CString * pCstr;
  193.  
  194.  while (     ! soundsList.IsEmpty() 
  195.          &&  (pCstr = (CString *)soundsList.RemoveHead()) != NULL)
  196.        delete pCstr;
  197. }
  198.  
  199.  
  200. BOOL CMainDialog::GetSysSound(CString& sysSound)
  201. {
  202.  BOOL ok = FALSE;
  203.  CComboBox * cb = (CComboBox *)GetDlgItem(ID_SYSTEMSOUNDS);
  204.  CString * s = (CString *)(soundsList.GetAt(soundsList.FindIndex(cb->GetCurSel())));
  205.  
  206.  
  207.  if (! soundsList.IsEmpty())
  208.     {
  209.      sysSound = *s;
  210.      ok = TRUE;
  211.     }
  212.  
  213.  return ok;
  214. }
  215.  
  216.  
  217. BOOL CMainDialog::GetIniString(CString section, CString key, CString& value)
  218. {
  219.  BOOL ok;
  220.  
  221.  ok = GetPrivateProfileString( section, 
  222.                                key, 
  223.                                "", 
  224.                                value.GetBuffer(256), 
  225.                                256, 
  226.                                iniFileName ) > 0;
  227.  
  228.  value.ReleaseBuffer();
  229.  
  230.  return ok;
  231. }
  232.  
  233.  
  234. BOOL CMainDialog::FlushWavFilesList()
  235. {
  236.  BOOL ok = TRUE;
  237.  
  238.  if (bFilesChanged  ||  bAutoRandomiseChanged)
  239.     if (! curSysSound.IsEmpty())
  240.        SaveWavFilesList(curSysSound);
  241.  
  242.  return ok;
  243. }
  244.  
  245.  
  246. BOOL CMainDialog::LoadWavFilesList(const CString& sysSound)
  247. {
  248.  BOOL ok = TRUE;
  249.  char str[20];
  250.  int ctr;
  251.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  252.  CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
  253.  CString wavName;
  254.  
  255.  
  256.  FlushWavFilesList();
  257.  
  258.  
  259.  lb->ResetContent();
  260.  
  261.  int qty = GetPrivateProfileInt(sysSound, "FileCount", 0, iniFileName);
  262.  
  263.  for ( ctr  = 1;
  264.        ctr <= qty;
  265.        ctr++ )
  266.      {
  267.       wsprintf(str, "SoundFile%d", ctr);
  268.  
  269.       if (GetIniString(sysSound, str, wavName))
  270.          lb->AddString(wavName);
  271.      }
  272.  
  273.  
  274.  but->SetCheck(GetPrivateProfileInt(sysSound, "Randomize", 0, iniFileName));
  275.  
  276.  
  277.  curSysSound = sysSound;
  278.  
  279.  bFilesChanged = FALSE;
  280.  bAutoRandomiseChanged = FALSE;
  281.  
  282.  return ok;
  283. }
  284.  
  285.  
  286. BOOL CMainDialog::SaveWavFilesList(const CString & sysSound)
  287. {
  288.  BOOL ok = TRUE;
  289.  int qty = 0;
  290.  int count = 0;
  291.  CListBox * lb = (CListBox *)GetDlgItem(ID_FILESLIST);
  292.  char key[30];
  293.  char value[10];
  294.  CString filename;
  295.  CButton * but = (CButton *)GetDlgItem(ID_AUTORANDOMISE);
  296.  
  297.  qty = lb->GetCount();
  298.  
  299.  if (qty == LB_ERR)
  300.     qty = 0;
  301.  
  302.  for ( count  = 1;
  303.        count <= qty;
  304.        count++ )
  305.      {
  306.       wsprintf(key, "SoundFile%d", count);
  307.  
  308.       lb->GetText(count - 1, filename);
  309.  
  310.       WritePrivateProfileString(sysSound, key, filename, iniFileName);
  311.